package com.coocaa.ops.admin.v1.auth.sys;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.Config;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.auth.sys.constant.MenuTokenConstants;
import com.coocaa.ops.admin.v1.auth.sys.exception.SysUserException;
import com.coocaa.ops.admin.v1.auth.sys.model.query.SysUserModel;
import com.coocaa.ops.admin.v1.auth.sys.service.impl.SysUserServiceImpl;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/13.
 * @description: 系统用户 控制器
 *
 */
@RestController
@Config(menuToken = MenuTokenConstants.SYS_USER)//菜单elid
@RequestMapping("/v1/sysUser")
public class SysUserController extends MgrBaseController {
    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public R save(HttpServletRequest request, String jsonStr) throws Exception {
        SysUserEntity entity = (SysUserEntity) JsonUtil.json2Object(jsonStr, SysUserEntity.class);
        try {
            sysUserService.save(entity, WebUtil.getCurrentUserId(request));
        } catch (SysUserException e) {
            return R.error(e.getCode().value().toString(), e.getMessage());
        }
        return R.ok();
    }

    /**
     * 获取用户没有角色列表
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getNotRolesByUserId",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getNotRolesByUserId(HttpServletRequest request, @RequestParam Integer userId) {
        Map result = sysUserService.getNotRolesByUserId(userId);
        return render2Json(result);
    }


    /**
     * 获取用户已有角色列表
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getRolesByUserId",method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public String getRolesByUserId(HttpServletRequest request, @RequestParam Integer userId) {
        Map result = sysUserService.getRolesByUserId(userId);
        return render2Json(result);
    }

    /**
     * 获取用户已经选择的业务组
     *
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDictCheckedByUserId",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public String getDictCheckedByUserId(HttpServletRequest request, @RequestParam Integer userId) {
        Map result = sysUserService.getDictCheckedByUserId(userId);
        return render2Json(result);
    }

    /**
     * 保存更新用户角色
     *
     * @param request
     * @param userId
     * @param roleIds
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/saveUserRoles",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object saveUserRoles(HttpServletRequest request, @RequestParam Integer userId, @RequestParam(value = "roleIds", required = false) Integer[] roleIds) {
        sysUserService.saveUserRoles(roleIds, userId);
        return R.ok();
    }

    /**
     * 设置数据权限保存
     * 未完待续
     *
     * @param request
     * @param userId
     * @param dicts
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/saveProfession",method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    public Object saveProfession(HttpServletRequest request, @RequestParam Integer userId, @RequestParam(value = "dicts", required = false) List<Integer> dicts) {
        sysUserService.saveProfession(dicts, userId);
        return R.ok();
    }

    /**
     * 获取详情信息
     *
     * @param id
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getDetailInfo",method = {RequestMethod.GET})
    public Object getDetailInfo(Integer id) {
        InputParamsValidator.validIdValue(id);
        SysUserEntity entity = sysUserService.getDetailInfo(id);
        Map result = R.ok(entity);
        return render2Json(result);
    }


    /**
     * 分页数据
     *
     * @param request
     * @param model
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/pageList",method = {RequestMethod.GET,RequestMethod.POST})
    public Object pageList(HttpServletRequest request, SysUserModel model) {
        Map<String, Object> data = sysUserService.getPageResult(model);
        return render2Json(data);
    }


    /**
     * 删除记录
     *
     * @param
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.DELETE)
    @RequestMapping(value = "/remove",method = {RequestMethod.GET,RequestMethod.POST})
    public Object remove(@RequestParam Integer[] userId, HttpServletRequest request) {
        return sysUserService.deleteBatch(userId, WebUtil.getCurrentUser(request));
    }

    /**
     * 获取菜单运行权限列表
     *
     * @param request
     * @param
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/getMenuInfo", method = {RequestMethod.GET}, produces = "text/html;charset=UTF-8")
    public Object getMenuInfo(HttpServletRequest request) {
        Map result = getMenuInfo(this.getClass(), request);
        return render2Json(result);
    }
}
